<%@ page import="com.feng.ALittleFlower.Database" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<%
    request.setCharacterEncoding("utf-8");
    String username = (String) request.getParameter("username");
    String password = (String) request.getParameter("password");
    String password1 = (String) request.getParameter("password1");
    String regExp = "^[A-Za-z0-9]{6,20}$"; // 正则表达式判断密码是否符合要求
    String usernameProblem = "";
    String passwordNotMatched = "";
    String passwordProblem = "";
    boolean passed = (username != null && password != null);
    if (username != null) {
        try {
            if (Database.isSQLUserExists(username)) {
                usernameProblem = "该用户名" + username + "已存在";
                passed = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (password != null && !password.equals(password1)) {
        passwordNotMatched ="两次密码不一致！";
        passed = false;
    }
    if (password != null && !password.matches(regExp)) {
        passwordProblem = "密码输入不符合要求,请使用6～20位数字或字母组合";
        passed = false;
    }

    if (passed) {
        try {
            Database.createAccount(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        session.setAttribute("username", username);
        response.sendRedirect("./index.jsp");
    }
%>
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="./css/bootstrap.css" rel="stylesheet">
    <link href="register.css" rel="stylesheet">
    <title>Hello, world!</title>
</head>

<body class="bg d-flex">
<%@include file="navbar.jsp"%>
<div class="container my-5 py-5">
    <div class="row">
        <div class="col-12 text-center">
            <div class="col-12 ">
                <h1 class="display-4 text-primary">注册</h1>
                <hr class="w-25 mx-auto">
            </div>
        </div>
    </div>
    <form class="form_container" method="post">
        <div class="mb-3">
            <input placeholder="请输入用户名" required type="text" class="form-control" name="username" id="username" aria-describedby="usernameHelp">
            <div id="usernameHelp" class="alert-danger"><%=usernameProblem%></div>
        </div>
        <div class="mb-3">
            <input type="password" required placeholder="请输入6-20位数字或字母密码" class="form-control" name="password" id="password" aria-describedby="passwordHelp">
            <div id="passwordHelp" class="alert-danger"><%=passwordProblem%></div>
        </div>
        <div class="mb-3">
            <input type="password" required placeholder="请再次输入密码" class="form-control" name="password1" id="password1" aria-describedby="password1Help">
            <div id="password1Help" class="alert-danger"><%=passwordNotMatched%></div>
        </div>

        <button type="submit" class="btn btn-primary btn-lg">注册</button>

    </form>
</div>





    <script src="js/popper.js"></script>
    <script src="js/bootstrap.js"></script>
</body>

</html>